package main

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"strconv"
	"time"
	"os"
	"flag"
)

var prefix = "bjpk10"
var formate="2006/1/02 15:04:05"

var sheetName = "1010"
// 2018.3.31  880058	2018-03-31 23:50:00
var  baseplay float64 =674080
var  baseplayDate="2018/03/31"

func main() {

	//  go run main.go -beginDate=2018/4/02 -days=31
	fmt.Println("run like this :  enter dir  ./sh11x5_darwin_amd64  -beginDate=`2018/4/01` -days=30")
	var days int
	flag.IntVar(&days,"days", 1, "days")
	var beginDate string
	flag.StringVar(&beginDate, "beginDate", "nil", "beginDate")
	flag.Parse()

	if "nil" ==beginDate  {
		fmt.Println("params -beginDate have to need.")
		os.Exit(-1)
	}
	for i := 1; i <= days; i++ {
		var formateyyymmdd="2006/1/02"
		local, _ := time.LoadLocation("America/Los_Angeles")
		timeNow, _ := time.ParseInLocation(formateyyymmdd,beginDate, local)
		timeDay1:=timeNow.Add(time.Duration((i-1)*24)*time.Hour)
		date:=timeDay1.Format(formateyyymmdd)
		fmt.Println("---->",date)
		excel(date);
	}
}


func excel(beginDate string){
	//os.Exit(-1)
	local, _ := time.LoadLocation("America/Los_Angeles")

	xlsx := excelize.NewFile()
	// Create a new sheet.
	index := xlsx.NewSheet(sheetName)
	if index>1 {
		xlsx.DeleteSheet("Sheet1")
	}
	// Set value of a cell.
	//xlsx.SetCellValue("Sheet2", "A2", "Hello world.")
	// Title
	xlsx.SetCellStr(sheetName, "A1", "期数")
	xlsx.SetCellStr(sheetName, "B1", "开始投注时间")
	xlsx.SetCellStr(sheetName, "C1", "开奖时间")
	xlsx.SetCellStr(sheetName, "D1", "是否最后一期")
	max := 180

	inputTime:=beginDate //输入需要生成的当天时间

	var formateyyymmdd="2006/1/02"
	timeNow, _ := time.ParseInLocation(formateyyymmdd,inputTime, local)
	timeOld, _ := time.ParseInLocation(formateyyymmdd,baseplayDate, local)

	timeA:=timeNow.Format("20060102")

	aa:=timeNow.Sub(timeOld);
	day:=aa.Hours()/24;
	//每天179期
	qi:=baseplay+(day-1)*179;

	fmt.Println(aa)
	b:=inputTime+"  9:02:00"//每天开始投注时间

	for i := 2; i <= max; i++ {
		nums:=strconv.Itoa(i)
		xlsx.SetCellValue(sheetName, "A"+nums,qi+float64((i-1)))

		timeNow, eeee := time.ParseInLocation(formate, b, local)
		if eeee!=nil {
			fmt.Println(eeee)
		}
		//bjpk10 第一次开奖时间为7分钟,后面是5分钟

		tim10:=timeNow.Add(time.Duration((i-2)*5)*time.Minute)
		if i==2 {
			tim10=tim10.Add(time.Duration(-2)*time.Minute)
		}
		xlsx.SetCellValue(sheetName, "B"+nums, tim10)

		tim20:=timeNow.Add(time.Duration((i-1)*5)*time.Minute)
		xlsx.SetCellValue(sheetName, "C"+nums, tim20)
		//0" "
		if i==max {
			xlsx.SetCellInt(sheetName, "D"+nums, 1)
		}else{
			xlsx.SetCellInt(sheetName, "D"+nums, 0)
		}
	}
	// Set active sheet of the workbook.
	xlsx.SetActiveSheet(index)

	// Save xlsx file by the given path.
	err := xlsx.SaveAs("./"+prefix+"_"+timeA+".xlsx")
	if err != nil {
		fmt.Println(err)
	}
}
